package com.ydh.company.config;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import com.ydh.company.filter.RolesOrFilter;
import com.ydh.company.realm.MyRealm;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author:wkl
 * @Date: 2020/4/23 10:25
 * @Version 0.0.1-SNAPSHOT
 */
@Configuration
public class ShiroConfig {

    @Bean
    public ShiroDialect shiroDialect(){
        return new ShiroDialect();
    }
    /**
     * 加密器
     * @return
     */
    @Bean
    public HashedCredentialsMatcher hashedCredentialsMatcher(){
        //密码加密：创建加密器
        HashedCredentialsMatcher matcher = new HashedCredentialsMatcher();
        //设置加密类型
        matcher.setHashAlgorithmName("md5");
        //设置加密次数
        matcher.setHashIterations(1);
        return matcher;
    }


    /**
     * 自定义的realm
     * @return
     */
    @Bean
    public MyRealm myRealm(){
        MyRealm myRealm = new MyRealm();
        //设置加密器
//        myRealm.setCredentialsMatcher(hashedCredentialsMatcher());
        return myRealm;
    }

    /**
     * 安全管理器
     * @return
     */
    @Bean
    public SecurityManager securityManager(){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        //设置realm
//        securityManager.setRealm(myRealm());
        return securityManager;
    }

   /* @Bean
    public ShiroFilterFactoryBean shiroFilter(){

        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        // 设置安全管理器: 将安全管理器设置为全局
        shiroFilterFactoryBean.setSecurityManager(securityManager());

        // 配置拦截器
        Map<String,String> filterChainDefinitionMap = new HashMap<>(16);
        // 配置不会被拦截的链接 顺序判断，因为前端模板采用了 thymeleaf，
        // 这里不能直接使用 ("/static/**", "anon")来配置匿名访问，必须配置到每个静态目录, 放行静态资源
        filterChainDefinitionMap.put("/css/**", "anon");
        filterChainDefinitionMap.put("/fonts/**", "anon");
        filterChainDefinitionMap.put("/images/**", "anon");
        filterChainDefinitionMap.put("/js/**", "anon");
        filterChainDefinitionMap.put("/lib/**", "anon");

        //配置退出 过滤器,其中的具体的退出代码 Shiro 已经替我们实现了
        filterChainDefinitionMap.put("/logout", "logout");

        // 过滤链定义，从上向下顺序执行，一般将/**放在最为下边 -->:这是一个坑呢，一不小心代码就不好使了;
        // authc:所有 url 都必须认证通过才可以访问; anon:所有 url 都都可以匿名访问-->
        filterChainDefinitionMap.put("/login/page","anon");
        filterChainDefinitionMap.put("/login/do","anon");

        //验证自带的角色过滤器
        //filterChainDefinitionMap.put("/member/**","rolesOr[超级管理员,会员管理员]");

        filterChainDefinitionMap.put("/**", "authc");
        // 如果不设置默认会自动寻找 Web 工程根目录下的"/login.jsp"页面
        shiroFilterFactoryBean.setLoginUrl("/login/page");
        // 登录成功后要跳转的链接
        shiroFilterFactoryBean.setSuccessUrl("/index");
        //未授权界面;
        shiroFilterFactoryBean.setUnauthorizedUrl("/403");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);

        //将自定义过滤器放入过滤器链中
        Map<String, Filter> filterMap = new HashMap<>(16);
        filterMap.put("rolesOr",rolesOrFilter());
        //注入自定义过滤器
        shiroFilterFactoryBean.setFilters(filterMap);

        return shiroFilterFactoryBean;
    }*/

    /**
     * 注入自定义的过滤器： roles or
     * @return
     */
    @Bean
    public RolesOrFilter rolesOrFilter(){
        return new RolesOrFilter();
    }

    /**
     * ShiroBean的生命周期管理，主要是注解管理
     * @return
     */
    @Bean
    public LifecycleBeanPostProcessor lifecycleBeanPostProcessor(){
        return new LifecycleBeanPostProcessor();
    }

    /**
     * 注解授权的支持（注解授权的切面代理）
     * @return
     */
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager){
        AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
        advisor.setSecurityManager(securityManager());
        return advisor;
    }
}
